﻿<!DOCTYPE html>
<html>
<head>
    <script src="../../js/header.js"></script>
</head>
<body>
    <section class="container-fluid" style="background-color: white;">
        <form class="form-horizontal m15_t" id="form">
            <div class="tab-content">
                <div class="tab-pane fade in active">
                    <div class="form-group" id="divShowOrderNo" style="display:none;">
                        <label class="wd100 control-label">订 单 号：</label>
                        <div class="wd250 m15_l">
                            <label class="form-control" id="labOrderNo"></label>
                        </div>
                    </div>

                    <div class="form-group" id="divCMName">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>卡商名称</label>
                        <select class="m15_l" name="sltCMName" id="sltCMName"></select>
                    </div>
                    <div class="form-group" id="divBankName">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>银行名称</label>
                        <select class="m15_l" name="sltBankName" id="sltBankName"></select>
                    </div>
                    <div class="form-group" id="divOrderQuantity">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>下单数量：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="orderQuantity" maxlength="10" />
                        </div>
                    </div>
                    <div class="form-group" id="divUnitPrice">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>单价：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="unitPrice" maxlength="10" />
                        </div>
                    </div>
                    <div class="form-group" id="divActualQuantity">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>实际数量：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="actualQuantity" maxlength="10" />
                        </div>
                    </div>
                    <div class="form-group" id="divActualUnitPrice">
                        <label class="wd100 control-label"><span class="WdateFmtErr">*</span>实际单价：</label>
                        <div class="wd250 m15_l">
                            <input class="form-control" type="text" name="actualUnitPrice" maxlength="10" />
                        </div>
                    </div>
                    <div class="form-group" id="divRemark">
                        <label class="wd100 control-label">备注：</label>
                        <div class="wd250 m15_l">
                            <textarea style="height: auto;font-size: 12px;width: 100%;" type="text" id="remark" name="remark" maxlength="200">
                             </textarea>
                        </div>
                    </div>
                </div>
            </div>
            <input type="hidden" id="hidOrderNo" />
            <input type="hidden" value="0" id="isCanEdit" />
        </form>
    </section>
    <script type="text/javascript">
        var validator = null;
        $(function () {
            showCardMerchant();
            showBankInfo();

            var detailID = FlashPay.Util.GetQueryString("detailID");
            if (detailID != null && detailID != undefined) {
                var type = FlashPay.Util.GetQueryString("type");
                var url = "/OrderDetail/Get";
                if (type == 2) {
                    url = "/OrderDetail/GetQuantityPrice";
                }
                showOrderDetailInfo(detailID,url);
            }

            validator = FlashPay.Util.Validator({
                formObj: $("#form"),
                rules: {
                    sltCMName: {
                        required: true
                    },
                    sltBankName: {
                        required: true
                    },
                    orderQuantity: {
                        required: true, digits: true, isIntGtZero: true
                    },
                    unitPrice: {
                        required: true, money: true, isFloatGtZero: true
                    },
                    actualQuantity: {
                        required: true, digits: true, isIntGtZero: true
                    },
                    actualUnitPrice: {
                        required: true, money: true, isIntGtZero: true
                    }
                },
                messages: {
                    sltCMName: {
                        required: "请选择卡商名称"
                    },
                    sltBankName: {
                        required: "请选择银行名称"
                    },
                    orderQuantity: {
                        required: "下单数量不能为空", isIntGtZero: "下单数量必须大于0", digits: "下单数量必须为数字"
                    },
                    unitPrice: {
                        required: "单价不能为空", money: "单价必须为金额，最多保留两位小数", isFloatGtZero: "单价必须大于0"
                    },
                    actualQuantity: {
                        required: "实际数量不能为空", digits: "实际数量必须为数字", isIntGtZero: "实际数量必须大于0"
                    },
                    actualUnitPrice: {
                        required: "实际价格不能为空", money: "实际价格必须为金额，最多保留两位小数", isFloatGtZero: "实际价格必须大于0"
                    },
                },
            });
        });

        //显示订单详情
        function showOrderDetailInfo(paras,url) {
            FlashPay.Util.Ajax({
                type: "POST",
                url: url,
                data: JSON.stringify({ detailID: paras }),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.totalCount > 0) {
                        var infos = data.result[0];

                        FlashPay.Util.SetInputVal("orderQuantity", infos.orderQuantity);
                        FlashPay.Util.SetInputVal("unitPrice", infos.unitPrice);
                        FlashPay.Util.SetInputVal("actualQuantity", infos.actualQuantity);
                        FlashPay.Util.SetInputVal("actualUnitPrice", infos.actualUnitPrice);
                        $("#remark").val(infos.remark);
                        $("#sltCMName").find("option[value=" + infos.cmid + "]").attr("selected", true);
                        $("#sltBankName").find("option[value=" + $.trim(infos.bankName) + "]").attr("selected", true);
                        $("#hidOrderNo").val(infos.orderNo);

                        var type = FlashPay.Util.GetQueryString("type");
                        if (parseInt(type) == 2) {
                            $("#divCMName").remove();
                            $("#divBankName").remove();
                            $("#divOrderQuantity").remove();
                            $("#divUnitPrice").remove();
                            $("#divRemark").remove();
                            $("#divShowOrderNo").show(); $("#labOrderNo").text(infos.orderNo);
                        }
                    }
                }
            });
        }

        //显示卡商下拉列表
        function showCardMerchant() {
            FlashPay.Util.Ajax({
                type: "Post",
                url: "/CardMerchant/GetList",
                data: JSON.stringify({ CMName: '' }),
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.success) {
                        var html = "";
                        var infos = data.data;
                        for (var i = 0; i < infos.length; i++) {
                            html += "<option value=" + infos[i].cmid + ">" + infos[i].cmname + "</option>";
                        }
                        $("#sltCMName").html('<option value="">请选择</option>').append(html);
                    }
                }
            });
        }

        //显示银行下拉列表
        function showBankInfo() {
            var data = { CurrentPageIndex: 1, PageSize: 900000 };
            FlashPay.Util.Ajax({
                type: "Get",
                url: "/BankInfo/GetList",
                data: data,
                async: false,
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    if (data.success) {
                        var html = "";
                        var infos = data.tData;
                        for (var i = 0; i < infos.length; i++) {
                            html += "<option value=" + infos[i].bankCode + ">" + infos[i].bankName + "</option>";
                        }
                        $("#sltBankName").html('<option value="">请选择</option>').append(html);
                    }
                }
            });
        }

        function save() {
            if ($("body").data("confirmReceive") == "lock") {
                return false;
            } else {
                $("body").data("confirmReceive", "lock");
            }
            if (!validator.form()) {
                return false;
            }

            var orderModel = {};
            var url = "/OrderDetail/Add";

            var type = FlashPay.Util.GetQueryString("type");
            if (type == 1) {
                url = "/OrderDetail/Edit";
            } else if (type == 2) {
                url = "/OrderDetail/EditActualCountAndUnitPrice";
                orderModel.actualQuantity = $('input[name="actualQuantity"]').val();
                orderModel.actualUnitPrice = $("input[name='actualUnitPrice']").val();
            }

            if (type <= 1) {
                orderModel.cmid = $("select[name='sltCMName']").val();
                orderModel.bankname = $('select[name="sltBankName"]').val();
                orderModel.orderQuantity = $("input[name='orderQuantity']").val();
                orderModel.unitPrice = $("input[name='unitPrice']").val();
                orderModel.remark = $.trim($("textarea[name='remark']").val());
                orderModel.actualQuantity = $('input[name="actualQuantity"]').val();
                orderModel.actualUnitPrice = $("input[name='actualUnitPrice']").val();
            }

            var detailID = FlashPay.Util.GetQueryString("detailID");
            if (detailID != null && detailID != undefined) {
                orderModel.detailID = detailID;
                orderModel.orderNo = $("#hidOrderNo").val();
            } else {
                orderModel.orderNo = FlashPay.Util.GetQueryString("orderNo");
            }

            FlashPay.Util.Ajax({
                type: "POST",
                url: url,
                data: JSON.stringify(orderModel),
                dataType: "json",
                contentType: "application/json",
                success: function (data) {
                    $("body").data("confirmReceive", "unlock");
                    if (!data.success) {
                        FlashPay.UI.Tip_short_danger(data.message == null ? "操作失败！" : data.message);
                        return;
                    } else {
                        FlashPay.UI.DialogOpener().Tip_success(data.message == null ? "操作成功！" : data.message);
                        FlashPay.UI.DialogOpener().doSearch();
                        FlashPay.UI.CloseDialog();
                    }
                }
            });
        }
    </script>
</body>
</html>